package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/golang/protobuf/ptypes/wrappers"
	"time"

	context2 "kop/framework/context"
	"kop/framework/errors"
	"kop/modules/award/models"
	"kop/modules/world_boss/client"
	"kop/pb"
	"kop/pb/service"

	captainC "kop/modules/captain/client"
	roleC "kop/modules/role/client"
)

type WorldBossService struct {
	context2.Context
}

func (s *WorldBossService) Main(ctx context.Context, _ *empty.Empty) (*pb.WorldBossReply, error) {

	var reply pb.WorldBossReply

	//0点~10点	休战状态	提督大人，目前海上风平浪静，没有发现异常
	//10点~12点	备战状态	提督大人，发现大批幽灵海盗来袭，预计达到时间：[精英模式开始剩余时间]
	//12点~18点	精英模式进行中	提督大人，大批幽灵海盗来袭，请立即支援
	//击退全部精英怪	提督大人，幽灵海盗已全部被我们击退了
	//18点~20点	备战状态	提督大人，挪威海怪即将来袭，预计达到时间：[首领模式开始剩余时间]
	//20点~21点	首领模式进行中	提督大人，挪威海怪来袭，请立即支援
	//其他玩家击退挪威海怪	前方传来捷报，挪威海怪被 [提督名称] 击退
	//玩家自己击退挪威海怪	提督大人，挪威海怪终于被我们击退了
	//21点~24点

	var roleID = s.GetRoleID(ctx)

	var hour = time.Now().Hour()
	if hour >= 0 && hour < 10 {
		return &reply, nil
	} else if hour >= 10 && hour < 18 {

		var res, err = client.WorldBossService.GetLittleBoss(context.Background(), &service.RoleRequest{RoleID: roleID})
		if err != nil {
			return &reply, err
		}
		reply.LittleBoss = &pb.LittleBoss{
			Level: res.Level,
		}
		reply.Boss = res.Boss

	} else if hour >= 18 {

		var err error
		reply.Boss, err = client.WorldBossService.GetWorldBoss(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: s.GetServerID(ctx)})
		if err != nil {
			return &reply, err
		}
	}

	return &reply, nil
}

func (s *WorldBossService) Attack(ctx context.Context, req *wrappers.Int32Value) (*pb.WorldBossAttackReply, error) {

	var reply pb.WorldBossAttackReply

	var roleID = s.GetRoleID(ctx)

	var role = roleC.GetRole(roleID)
	var captain = captainC.GetCaptain(roleID, req.Value)

	var hour = time.Now().Hour()
	if hour >= 12 && hour < 18 {

		var res, err = client.WorldBossService.AttackLittleBoss(ctx, &service.AttackLittleBossRequest{Role: role, CaptainID: captain.CaptainID, Damage: captain.Power})
		if err != nil {
			return &reply, err
		}

		reply.Success = true
		reply.Kill = res.Kill
		reply.Boss = res.Boss
		reply.Points = res.Points
		reply.Award = res.Award

		if reply.Award != nil {
			return &reply, models.SendAward(role, reply.Award)
		} else {
			return &reply, nil
		}

	} else if hour == 20 {

		var boss, err = client.WorldBossService.GetWorldBoss(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: s.GetServerID(ctx)})
		if err != nil {
			return &reply, err
		}

		reply.Boss = &pb.WorldBossReply{Boss: boss}
		if boss.HP == 0 {

			return &reply, nil
		} else {

			res, err := client.WorldBossService.AttackWorldBoss(ctx,
				&service.AttackWorldBossRequest{Role: role, CaptainID: captain.CaptainID, Damage: captain.Power})
			if err != nil {
				return &reply, err
			}

			reply.Success = res.Success
			if res.Success {
				reply.Kill = res.Kill
				reply.Boss = res.Boss
				reply.Points = res.Points
				reply.Award = res.Award
				return &reply, models.SendAward(role, reply.Award)
			} else {
				return &reply, nil
			}
		}
	} else {
		return &reply, errors.ParamError
	}
}

func (*WorldBossService) Resurrection(ctx context.Context, req *wrappers.Int32Value) (*pb.WorldBoss, error) {

	return client.WorldBossService.Resurrection(ctx, req)
}

func (*WorldBossService) LBossMessages(ctx context.Context, req *empty.Empty) (*pb.LBossMessages, error) {

	return client.WorldBossService.LBossMessages(ctx, req)
}
